﻿// Fill out your copyright notice in the Description page of Project Settings.


#include "DesignPatterns/FlyweightPattern.h"
#include "Kismet/KismetSystemLibrary.h"

void AFlyweightTestActor::BeginPlay()
{
	Super::BeginPlay();

	// 享元工厂实例
	m_pFlyweightFactory = GetWorld()->SpawnActor<AFlyweightFactory>(AFlyweightFactory::StaticClass());
	m_pFlyweightFactory->Init();

	// 定时器
	GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &AFlyweightTestActor::AutoSpawner, 0.4f, true);
}

void AFlyweightTestActor::AutoSpawner()
{
	// 随机位置
	int32 RandomInt = UKismetMathLibrary::RandomInteger(16);
	FVector RandomPos = FVector::ZeroVector + FVector(1, 0, 0) * (RandomInt / 4) + FVector(0, 1, 0) * (RandomInt % 4);
	// 获取随机空闲的地鼠或炸弹
	AFlyweightObject* FlyweightObj = m_pFlyweightFactory->GetRandomElement();
	if (FlyweightObj)
	{
		// 初始化享元对象
		FlyweightObj->Init(RandomPos);

		// 假定一定概率击中
		if (UKismetMathLibrary::RandomFloat() < 0.2)
		{
			FLatentActionInfo LatentActionInfo;
			UKismetSystemLibrary::Delay(GetWorld(), 0.1, LatentActionInfo);
			FlyweightObj->Hit();
		}
	}
}
